package controllers

import (
	"crypto/sha1"
	"fmt"
	"github.com/gin-gonic/gin"
	"html/template"
	"io"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testGin/model/mysql"
	"testGin/model/tool"
	"time"
)

type UploadController struct {
}

func (ctr UploadController) Index(c *gin.Context)  {

	c.HTML(200, "system/upload/index.html", gin.H{

	})
}
func (ctr UploadController) List(c *gin.Context)  {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("ctrinId")
	if ctrinId != "" {
		where = tool.MapWhere("id_ctrin", "=", ctrinId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account", "=", account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name", "=", name)
	}
	result, err := mysql.Files{}.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}
func (ctr UploadController) Flist(c *gin.Context)  {


	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "24")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("ctrinId")
	if ctrinId != "" {
		where = tool.MapWhere("id_ctrin", "=", ctrinId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account", "=", account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name", "=", name)
	}
	result, err := mysql.Files{}.GetListPage(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}

	c.HTML(200, "system/upload/flist.html", gin.H{
         "data":result["data"],
         "page":template.HTML(result["page"].(string)),
	})
}

func (ctr UploadController) AddAjax(c *gin.Context) {

	file, _ := c.FormFile("file")

	extArray := strings.Split(file.Filename, ".")

	tempName := tool.Md5Str(file.Filename + time.ANSIC + "." + extArray[1])

	path := time.Now().Format("2006-01-02")

	newFileName := tempName+"."+extArray[1]

	realpath := "./static/upload/"

	fileModel := mysql.Files{}


	admin := tool.SessionGet(c,"admin");

	fileModel.IdAdmin = admin.(mysql.Admins).Id
	fileModel.Name = file.Filename
	fileModel.Md5 = tempName
	fileModel.Savename = tempName
	fileModel.Savepath = path + "/" + newFileName
	fileModel.ImageUrl = "/static/upload/"+fileModel.Savepath
	fileModel.Ext = extArray[1]
	fileModel.Mime = file.Header.Get("Content-Type")
	fileModel.Size = file.Size

	t := sha1.New();
	io.WriteString(t,file.Filename);
	fileModel.Sha1 = fmt.Sprintf("%x",t.Sum(nil));

	mysql.Files{}.Add(fileModel)

	patherr := os.MkdirAll(filepath.Join(realpath,path), os.ModePerm)

	saveFilePath := realpath+path+"/"+newFileName

	if patherr!= nil {
		tool.Error(c,1,patherr.Error())
		return
	}

	err := c.SaveUploadedFile(file, saveFilePath)
	if err!= nil {
		tool.Error(c,1,err.Error())
		return
	}

	tool.Success(c,200,"上传成功",gin.H{
		"url": fileModel.ImageUrl,
	})
}
